<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta name="generator" content="Doxygen 1.8.13">
<title>OpenCV:图像监视:在Visual Studio debugger中查看内存中的图像</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon">
<link href="../../tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css">
<link href="../../stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">图像监视:在 Visual Studio 调试器中查看内存中的图像</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>上一篇教程:</b> <a class="el" href="../../dd/d6e/tutorial_windows_visual_studio_opencv.html">How to build applications with OpenCV inside the "Microsoft Visual Studio"</a></p>
<p><b>下一个教程:</b> <a class="el" href="../../d9/d52/tutorial_java_dev_intro.html">Introduction to Java Development</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">原作者</td><td align="left">Wolf Kienzle</td></tr>
<tr>
<td align="right">兼容性</td><td align="left">OpenCV &gt;= 3.0</td></tr>
</table>
<dl class="section warning"><dt>Warning</dt><dd>本教程可能包含过时的信息.</dd></dl>
<p>Image Watch是Microsoft Visual Studio的一个插件，它允许您可视化内存中的图像(<em><a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">cv::Mat</a></em>或<em>IplImage_</em>对象)，而调试应用程序。这有助于跟踪bug，或者简单地理解给定的代码段在做什么。</p>
<h2>先决条件</h2>
<p>本教程假设您具有以下可用功能:</p>
<ol type="1">
<li>安装了更新1的Visual Studio 2012 Professional(或更高版本)。可以下载更新1<a href="http://www.microsoft.com/en-us/download/details.aspx?id=35774">在这里</a>.</li>
<li>在Windows计算机上安装OpenCV(教程:<a class="el" href="../../d3/d52/tutorial_windows_install.html">Installation in Windows</a>).</li>
<li>能够在Visual Studio中创建和构建OpenCV项目(教程:<a class="el" href="../../dd/d6e/tutorial_windows_visual_studio_opencv.html">How to build applications with OpenCV inside the "Microsoft Visual Studio"</a>).</li>
</ol>
<h2>安装</h2>
<p>下载图像监视安装程序(<a href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch2019">Visual Studio 2019</a> | <a href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch2017">Visual Studio 2017</a> | <a href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch">Visual Studio 2012、2013、2015</a>)安装程序包含一个扩展名为.vsix的文件(<em>Visual Studio 扩展</em>). 要启动它，只需在Windows资源管理器中双击.vsix文件。安装程序完成后，请确保重新启动Visual Studio以完成安装。</p>
<h2>Example(例子)</h2>
<p>Image Watch适用于任何使用OpenCV图像对象的现有项目(例如，<em><a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">cv::Mat</a></em>). 在这个例子中，我们使用一个最小的测试程序，从一个文件中加载一个图像并运行一个边缘检测器。要构建该程序，请在visualstudio中创建一个控制台应用程序项目，将其命名为“imagewatchdemo”，并在下面插入源代码。</p><div class="fragment"><div class="line"><span class="comment">// Test application for the Visual Studio Image Watch Debugger extension</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span>                        <span class="comment">// std::cout</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d2/ddb/core_2include_2opencv2_2core_2core_8hpp.html">opencv2/core/core.hpp</a>&gt;</span>           <span class="comment">// cv::Mat</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d7/dd8/imgcodecs_2imgcodecs_8hpp.html">opencv2/imgcodecs/imgcodecs.hpp</a>&gt;</span>     <span class="comment">// cv::imread()</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dd/d01/imgproc_2include_2opencv2_2imgproc_2imgproc_8hpp.html">opencv2/imgproc/imgproc.hpp</a>&gt;</span>     <span class="comment">// cv::Canny()</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> help()</div><div class="line">{</div><div class="line">    cout</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------&quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;This is a test program for the Image Watch Debugger &quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;plug-in for Visual Studio. The program loads an     &quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;image from a file and runs the Canny edge detector. &quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;No output is displayed or written to disk.&quot;</span></div><div class="line">        &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;Usage:&quot;</span>                                               &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;image-watch-demo inputimage&quot;</span>                          &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------&quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">    help();</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (argc != 2)</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Wrong number of parameters&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Loading input image: &quot;</span> &lt;&lt; argv[1] &lt;&lt; endl;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> input;</div><div class="line">    input = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(argv[1], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line"></div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Detecting edges in input image&quot;</span> &lt;&lt; endl;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> edges;</div><div class="line">    <a class="code" href="../../dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de">Canny</a>(input, edges, 10, 100);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><p>确保您的活动解决方案配置(Build&ndash;&gt;配置管理器)设置为调试版本(通常称为“调试”)。这将禁用编译器优化，以便在调试器中查看变量可以可靠地工作。</p>
<p>构建您的解决方案(Build&ndash;&gt;生成解决方案，或按<em>F7</em>).</p>
<p>在继续之前，不要忘记将输入图像的命令行参数添加到项目中(右键单击project&ndash;&gt;属性&ndash;&gt;配置属性&ndash;&gt;调试，然后使用映像的位置设置字段命令参数)。</p>
<p>现在在源代码行上设置一个断点</p><div class="fragment"><div class="line">Mat edges;</div></div><!-- fragment --><p>要设置断点，请右键单击源代码行并选择断点&ndash;&gt;从上下文菜单中插入断点。</p>
<p>在调试器中启动程序(Debug&ndash;&gt;开始调试，或点击<em>F5</em>). 当到达断点时，程序将暂停，Visual Studio将在断点处显示黄色指令指针:</p>
<div class="image">
<img src="../../breakpoint.png" alt="breakpoint.png">
</div>
<p>现在可以检查程序的状态。例如，你可以把<em>局部变量</em>窗口(调试&ndash;&gt;窗口&ndash;&gt;局部变量)，它将显示当前作用域中变量的名称和值:</p>
<div class="image">
<img src="../../vs_locals.png" alt="vs_locals.png">
</div>
<p>请注意，内置<em>局部变量</em>窗口将只显示文本。这就是图像手表插件的用武之地。图像手表就像另一个<em>局部变量</em>窗口，但内置了图像查看器。要打开图像监视，请选择查看&ndash;&gt;其他窗口&ndash;&gt;图像监视。像visualstudio的<em>局部变量</em>窗口，图像观看可以停靠到Visual Studio IDE。另外，VisualStudio还将记住是否打开了图像监视，以及它在调试会话之间的位置。这意味着您只需执行一次;下次开始调试时，“图像监视”将回到原来的位置。以下是停靠图像监视窗口在断点处的外观:</p>
<div class="image">
<img src="../../toolwindow.jpg" alt="toolwindow.jpg">
</div>
<p>左上角的单选按钮(<em>本地人/观察</em>)选择显示在<em>图像列表</em>下图:<em>局部变量</em>列出当前范围内的所有OpenCV图像对象(此列表将自动填充)。<em>观察</em>显示已固定以进行连续检查的图像表达式(此处未描述，请参阅)<a href="http://go.microsoft.com/fwlink/?LinkId=285461">图像监视文档</a>详细信息)。图像列表显示基本信息，如宽度、高度、通道数，以及缩略图(如果可用)。在我们的示例中，图像列表包含两个局部图像变量，<em>输入</em>和<em>边缘</em>.</p>
<p>如果图像有缩略图，左键单击该图像将在<em>图像查看器</em>在右边。查看器允许您平移(拖动鼠标)和缩放(鼠标滚轮)。它还显示当前鼠标位置的像素坐标和值。</p>
<div class="image">
<img src="../../viewer.jpg" alt="viewer.jpg">
</div>
<p>注意列表中的第二个图像，<em>边缘</em>，显示为“无效”。这表示此图像对象的某些数据成员的值已损坏或无效(例如，负图像宽度)。这是程序中的这一点，因为C++构造函数<em>边缘</em>尚未运行，因此其成员具有未定义的值(在调试模式下，它们通常用“0xCD”字节填充)。</p>
<p>从这里您可以单步完成代码(调试-&gt；跨过，或按<em>F10</em>)观察像素的变化:如果你一步一个脚印<em>Mat edges;</em>声明<em>边缘</em>图像将从“invalid”变为“empty”，这意味着它现在处于有效状态(默认构造)，即使它尚未初始化(使用<em><a class="el" href="../../d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0" title="Allocates new array data if needed. ">cv::Mat::create()</a></em>，例如)。如果你再跨一步<em><a class="el" href="../../dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de" title="Finds edges in an image using the Canny algorithm  . ">cv::Canny()</a></em>调用时，您将看到边缘图像的缩略图出现在图像列表中。</p>
<p>现在假设您想对<em><a class="el" href="../../dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de" title="Finds edges in an image using the Canny algorithm  . ">cv::Canny()</a></em>实施。带上<em>边缘</em>通过在<em>图像列表</em>并放大具有清晰边界的区域:</p>
<div class="image">
<img src="../../edges_zoom.png" alt="edges_zoom.png">
</div>
<p>右键单击<em>图像查看器</em>打开视图上下文菜单并启用链接视图(菜单项旁边的复选框指示是否启用该选项)。</p>
<div class="image">
<img src="../../viewer_context_menu.png" alt="viewer_context_menu.png">
</div>
<p>在相同大小的图像之间翻转时，“链接视图”功能使视图区域保持固定。要了解其工作原理，请从图像列表中选择输入图像;现在，您应该可以在输入图像中看到相应的放大区域:</p>
<div class="image">
<img src="../../input_zoom.png" alt="input_zoom.png">
</div>
<p>也可以使用上/下光标键在查看输入和边之间来回切换。这样您就可以很容易地验证检测到的边缘是否与输入图像中的数据很好地对齐。</p>
<h2>More(更多)...</h2>
<p>图像手表有许多更先进的功能，如</p>
<ol type="1">
<li>将图像固定到<em>Watch</em>用于跨作用域或调试会话之间检查的列表</li>
<li>直接在监视窗口内钳制、阈值化或区分图像</li>
<li>将内存中的图像与文件中的参考图像进行比较</li>
</ol>
<p>请参考在线<a href="http://go.microsoft.com/fwlink/?LinkId=285461">图像监视文档</a>详情;您也可以通过单击<em>Help(帮助)</em>图像监视窗口中的链接:</p>
<div class="image">
<img src="../../help_button.jpg" alt="help_button.jpg">
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"><address class="footer"><small>Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen">
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
